<!-- This query gets the position of book in terms of their price - so, looping over a sequence that has already been narrowed-->
<bookcatalog>
{
<!-- first assigning the sequence ordered by price to a variable $bookList -->
let $booklist := (
<!-- for clause now iterates over $booklist and second return clause returns a new book element that includes a new element $priceIndex-->
for $book at $index in doc("catalog.xml")/bookcatalog/section/book
let $price := $book/price
let $genre := $book/genre
where $genre = "Fantasy"
order by $price descending
return
<book index = "{$index}">
{$book/@*}
{$book/*}
</book>
)
return
<!-- using if/then/else to return only the top 3 books-->
    for $filteredBook at $priceIndex in $booklist
    return
    if($priceIndex <=3) then
    <book priceIndex = "{$priceIndex}">
    {$filteredBook/@*}
    {$filteredBook/*}
    </book>

else 
''
}
</bookcatalog>
